package br.com.dispositivosmoveis.acompanhamentodeconta.ui.activity;

import java.util.List;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import br.com.dispositivosmoveis.acompanhamentodeconta.R;
import br.com.dispositivosmoveis.acompanhamentodeconta.Adapters.ItemListAdapter;
import br.com.dispositivosmoveis.acompanhamentodeconta.abstracts.AbstractActivity;
import br.com.dispositivosmoveis.acompanhamentodeconta.abstracts.SharedPreferencesService;
import br.com.dispositivosmoveis.acompanhamentodeconta.model.Item;
import br.com.dispositivosmoveis.acompanhamentodeconta.service.ItemServiceImpl;
import br.com.dispositivosmoveis.acompanhamentodeconta.service.SharedPreferencesServiceImpl;
import br.com.dispositivosmoveis.acompanhamentodeconta.util.Constantes;

public class ConsultarItemActivity extends
		AbstractActivity<Item, ItemServiceImpl> {
	
	SharedPreferencesService sharedPreferencesService;
	private ItemListAdapter itemListAdapter;
	private ListView listItens;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_consultar_item);
		sharedPreferencesService = new SharedPreferencesServiceImpl(this);
		
		this.listItens = (ListView) findViewById(R.id.listItens);
		
		startEvents();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.consultar_item, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void startEvents() {
		itemListAdapter = new ItemListAdapter(this, R.layout.row,
				this.getItens());
		listItens.setItemsCanFocus(false);
		listItens.setAdapter(itemListAdapter);
	}
	
	private List<Item> getItens(){
		return this.getService().getItemsByComanda(Integer.valueOf(sharedPreferencesService.obterValor(Constantes.ID_COMANDA_KEY)));
	}
}
